package com.mgxy;

import java.awt.*;

public class BallComponent implements MyComponent {

    //定义球的名称
    private  String name;
    //定义球的图片
    private Image ballimg;
    //定义白球的运动水平方向默认坐标
    private double x;
    //定义白球的垂直运动方向的坐标
    private double y;
    //定义成员变量 白球的移动速度
    private double speed;
    //设置白球的移动角度
    private double degree;
    //主窗口的引用
    private BallGame frame;

    public BallComponent(){
    }

    public BallComponent(String name, double x, double y, double speed, double degree, BallGame frame) {
        this.name = name;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.degree = degree;
        this.frame = frame;
    }

    public Image getBallimg() {
        return ballimg;
    }

    public void setBallimg(String ballurl) {
        this.ballimg = Toolkit.getDefaultToolkit().getImage(ballurl);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    public double getDegree() {
        return degree;
    }

    public void setDegree(double degree) {
        this.degree = degree;
    }

    public BallGame getFrame() {
        return frame;
    }

    public void setFrame(BallGame frame) {
        this.frame = frame;
    }

    @Override
    public void paintSelf(Graphics g) {
        //绘制白球
        g.drawImage(ballimg,(int) x,(int) y,null);
        if (speed > 0){
            //每次调用paint方法speed递减0.15，实现白球逐渐停止的效果
            speed -= 0.15;
            //x的运动到逐渐停止
            x += speed * Math.cos(degree);
            y += speed * Math.sin(degree);
        }

        //碰到上下边缘
        if(y<60 || y>440){
            degree = -degree;
        }
        //碰到左右边缘
        if(x<30 || x>800){
            degree = Math.PI - degree;
        }
    }
}
